package com.pb.camera.more;

import java.util.Observable;
import java.util.Observer;

import com.pb.camera.R;
import com.pb.camera.utils.TaskHelper;
import com.pb.camera.work.ControlTask;
import com.pb.camera.work.ReceiveTask;
import com.tutk.IOTC.AVIOCTRLDEFs;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class UpdateActivity extends Activity implements Observer {

    private TextView mTVCurrentVersion;
    private TextView mTVLastestVersion;
    private Button mBtnUpdate;
    private String mUpdateUrl;
    private String mModel;
    private String mCurrentVersion;
    private String mLastestVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        setContentView(R.layout.activity_frimware);
        ((TextView) findViewById(R.id.tv_title)).setText(R.string.update_firmware);
        mTVCurrentVersion = (TextView) findViewById(R.id.tv_current_version);
        mTVLastestVersion = (TextView) findViewById(R.id.tv_lastest_version);
        mBtnUpdate = (Button) findViewById(R.id.btn_update);
        mBtnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mUpdateUrl != null) {
                    TaskHelper.exec(new TaskHelper.Task() {
                        @Override
                        public void execute() throws Exception {
                            ControlTask.checkUpdateStatus(0, mModel, mLastestVersion, mUpdateUrl);
                        }

                        @Override
                        public void callback(Exception e) {

                        }
                    });
                }
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        mTVCurrentVersion.setText(intent.getStringExtra("currentVerison"));
        Bundle bundleInfo = intent.getBundleExtra("updateInfo");
        if (bundleInfo != null) {
            mLastestVersion = bundleInfo.getString("lastestVersion");
            mTVLastestVersion.setText(mLastestVersion);
            mUpdateUrl = bundleInfo.getString("downloadUrl");
            mModel = bundleInfo.getString("model");
            mBtnUpdate.setEnabled(true);
            ReceiveTask.getDefault().addObserver(this);
        } else {
            mTVLastestVersion.setText(intent.getStringExtra("currentVerison"));
        }
    }

    public void finishActivity(View v) {
        finish();
    }

    @Override
    public void update(Observable observable, final Object data) {
        TaskHelper.exec(new TaskHelper.Task() {
            @Override
            public void execute() throws Exception {
                if (data instanceof AVIOCTRLDEFs.ACCESS_OTA_FILE_CONFIG_RESP_CLASS
                        && ((AVIOCTRLDEFs.ACCESS_OTA_FILE_CONFIG_RESP_CLASS) data).result == 0) {
                    ControlTask.downLoadFile(0);
                }
                if (data instanceof AVIOCTRLDEFs.ACCESS_OTA_FILE_DOWNLOAD_RESP_CLASS && ((AVIOCTRLDEFs.ACCESS_OTA_FILE_DOWNLOAD_RESP_CLASS) data).result == 0) {
                    ControlTask.checkDownloadStatus(0);
                }
                if (data instanceof AVIOCTRLDEFs.ACCESS_OTA_FILE_OK_CHECK_RESP_CLASS) {
                    if (((AVIOCTRLDEFs.ACCESS_OTA_FILE_OK_CHECK_RESP_CLASS) data).result == 0) {
                        ControlTask.checkDownloadStatus(0);
                    } else if (((AVIOCTRLDEFs.ACCESS_OTA_FILE_OK_CHECK_RESP_CLASS) data).result == 1) {
                        ControlTask.processUpdate(0);
                    }
                }
                if (data instanceof AVIOCTRLDEFs.ACCESS_OTA_DEVICE_UPDATE_RESP_CLASS && ((AVIOCTRLDEFs.ACCESS_OTA_DEVICE_UPDATE_RESP_CLASS) data).result == 1) {
                    ControlTask.checkUpdateStatus(0);
                }
                if (data instanceof AVIOCTRLDEFs.ACCESS_OTA_DEVICE_UPDATE_CHECK_RESP_CLASS) {
                    if (((AVIOCTRLDEFs.ACCESS_OTA_DEVICE_UPDATE_RESP_CLASS) data).result == 1) {
                        //完成升级
                    } else if (((AVIOCTRLDEFs.ACCESS_OTA_DEVICE_UPDATE_RESP_CLASS) data).result == 0) {
                        ControlTask.checkUpdateStatus(0);
                    }
                }
            }
            @Override
            public void callback(Exception e) {
            }
        });
    }
}
